package library.sdk.demo.activity.module.qbox8;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.mapbox.mapboxsdk.geometry.LatLng;
import com.zhd.communication.DeviceManager;
import com.zhd.communication.HpcDiffManager;
import com.zhd.communication.object.EnumConnectStatus;
import com.zhd.communication.object.GpsPoint;

import java.text.DecimalFormat;

import library.sdk.demo.parent.AbsMapActivity;
import mapbox.MapUtils;

/**
 * Created by apple on 2017/5/26.
 */

public class QBox8Activity extends AbsMapActivity {
    private GPSInfoThread gpsPointThread;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        MapFragment.install(this, R.id.activity_map_frame);
        mConn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), BluetoothConnectActivity.class);
                startActivityForResult(intent, 0x0101);
            }
        });


        mStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isLogging = true;
                if (gpsPointThread == null) {
                    gpsPointThread = new GPSInfoThread();
                    gpsPointThread.start();
                }
            }
        });
    }


    /**
     * 开启线程获取GPS信息
     */
    private class GPSInfoThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);

                    final GpsPoint gpsPoint = DeviceManager.getInstance().getPosition();
                    if (gpsPoint != null) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateUI(gpsPoint);
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(QBox8Activity.this, "gpsPoint is null", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    mTextViewInfo.setText("数据获取失败");
                    Toast.makeText(QBox8Activity.this, e.toString(), Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }
    }

    private void updateUI(GpsPoint gpsPoint) {

        //TODO
        LatLng latLng = MapUtils.getLatLonFromWGS84(Math.toDegrees(gpsPoint.B), Math.toDegrees(gpsPoint.L));
        double longitude = latLng.getLongitude();
        double latitude = latLng.getLatitude();

        String nAccuracy = new DecimalFormat("0.0000").format(gpsPoint.XRms) + "," +
                new DecimalFormat("0.0000").format(gpsPoint.YRms) + ","
                + new DecimalFormat("0.0000").format(gpsPoint.HRms);

        String mode = "";
        mTextViewInfo.setText("longitude = " + longitude + "\n"
                        + "latitude = " + latitude + "\n"
                        + "卫星数 = " + gpsPoint.UsedSats +  "\n"
                        + "解类型= " + (mode = Util.getGpsQualityString(this, gpsPoint.Quality)) + "\n"
//                + "基站ID = " + gpsPoint.DiffStationID + "\n"
//                + "差分龄期 = " + gpsPoint.DiffAge + " s" + "\n"
                        + " 中误差x = " + new DecimalFormat("0.0000").format(gpsPoint.XRms) + " m" + "\n"
                        + "中误差y = " + new DecimalFormat("0.0000").format(gpsPoint.YRms) + " m" + "\n"
                        + "中误差h = " + new DecimalFormat("0.0000").format(gpsPoint.HRms) + " m" + "\n"
                        + "差分站:" + getRtkState()
//                + "UTC时间 = " + Util.getTimeString(gpsPoint.Utc) + "\n"
//                + "HDOP = " + gpsPoint.HDop + "" + "\n"

        );
        addMarker(new LatLng(latitude, longitude, gpsPoint.H), mode, nAccuracy);

    }

    public String getRtkState() {
        EnumConnectStatus status = HpcDiffManager.getInstance().getConnectStatus();
        if (status == EnumConnectStatus.CONNECTED) {
            return "已连接";
        } else if (status == EnumConnectStatus.CONNECTING) {
            return "正在连接";
        } else if (status == EnumConnectStatus.RECONNECTING) {
            return "正在重连";
        }
        return "未连接";
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x0101) {
            if (resultCode == RESULT_OK) {
                mConn.setText("已连接");
                //连接设备后连接查分服务器
                QBox8Activity.this.startActivity(new Intent(QBox8Activity.this, SetRoverActivity.class));

            }
        }
    }

    @Override
    public void finish() {
        super.finish();
        if (gpsPointThread != null) {
            gpsPointThread.interrupt();
        }
    }
}
